package com.yeshine.opendata.utils;


import com.yeshine.opendata.core.EntityBase;

import java.util.List;

/**
 * Created by taohj on 2014/4/17.
 * 分页查询工具类
 */
public class    PageResult<T extends EntityBase> extends JsonResult {


    private int pagination_size = 10;

    //当前页
    private Integer page;

    //每页大小
    private Integer size;

    //总记录数
    private int total;


    //查询结果
    private List<T> rows;


    //条件参数
    private T t;


    //排序 列
    private String sortColumn;

    //排序 正序 asc 或 逆序 desc
    private String sortBy;


    private String key;


    private String where;


    public PageResult() {
    }



    public PageResult(Integer page, Integer size) {
        this.page = page;
        this.size = size;
    }

    public Integer getPage() {
        return page;
    }

    public void setPage(Integer page) {
        this.page = page;
    }

    public Integer getSize() {
        return size;
    }

    public void setSize(Integer size) {
        this.size = size;
    }

    public int getTotal() {
        return total;
    }

    public void setTotal(int total) {
        this.total = total;
    }

    public List<T> getRows() {
        return rows;
    }

    public PageResult<T> setRows(List<T> rows) {
        this.rows = rows;
        return this;
    }

    public T getT() {
        return t;
    }

    public void setT(T t) {
        this.t = t;
    }


    public String getSortColumn() {
        return sortColumn;
    }

    public void setSortColumn(String sortColumn) {
        this.sortColumn = sortColumn;
    }

    public String getSortBy() {
        return sortBy;
    }

    public void setSortBy(String sortBy) {
        this.sortBy = sortBy;
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public String getWhere() {
        return where;
    }

    public void setWhere(String where) {
        this.where = where;
    }

    public int getMaxPage() {

        size=size==null||size==0?20:size;
        return total % size == 0 ? total / size : (total / size) + 1;
    }

    public int getStartPage() {


        int page=this.page;
        int maxPage=getMaxPage();
        page = page > maxPage ? maxPage : page;

        if (page <= pagination_size) {
            return 1;
        } else {
            return page % pagination_size == 0 ? page - pagination_size + 1 : ((pagination_size - page % pagination_size) + page - pagination_size)+1;
        }

    }



    public int getEndPage() {

        int page=this.page;

        int endPage=1;

        int maxPage=getMaxPage();

        page = page > maxPage ? maxPage : page;

        if (page <= pagination_size) {
            endPage= pagination_size;
        } else {
            endPage= page % pagination_size == 0 ? page : (pagination_size - page % pagination_size) + page;
        }
        return  endPage>maxPage?maxPage:endPage;
    }
}
